package com.frxuexi.mdm_2207.demo

import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.border
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.ExperimentalLayoutApi
import androidx.compose.foundation.layout.FlowRow
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.offset
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.CheckCircle
import androidx.compose.material.icons.filled.DateRange
import androidx.compose.material3.Button
import androidx.compose.material3.DatePicker
import androidx.compose.material3.DatePickerDialog
import androidx.compose.material3.Divider
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.Icon
import androidx.compose.material3.OutlinedTextField
import androidx.compose.material3.Text
import androidx.compose.material3.TextField
import androidx.compose.material3.rememberDatePickerState
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
//import androidx.constraintlayout.compose.ConstraintLayout
import com.frxuexi.mdm_2207.R
import java.text.SimpleDateFormat

@OptIn(ExperimentalLayoutApi::class, ExperimentalMaterial3Api::class)
@Preview
@Composable
fun Demo02() {

    val list = listOf<String>(
        "Price: High to Low",
        "Avg rating: 4+",
        "Free breakfast",
        "Free cancellation",
        "£50 pn",
        "Price: High to Low",
        "Avg rating: 4+",
        "Free breakfast",
        "Free cancellation",
        "£50 pn"
    )

    Column {
        Box(contentAlignment = Alignment.BottomEnd) {
            Image(
                painter = painterResource(id = R.drawable.ic_launcher_background),
                contentDescription = "",
                Modifier.clip(CircleShape)
            )
            Icon(
                imageVector = Icons.Default.CheckCircle,
                contentDescription = "",
                Modifier.offset(-10.dp, -2.dp)
            )
        }
        Divider(Modifier.width(1.dp).height(20.dp))

        /*ConstraintLayout(modifier = Modifier.fillMaxWidth()) {
            val (btn, txt) = createRefs()

            Button(onClick = { *//*TODO*//* }, modifier = Modifier.constrainAs(btn) {
                top.linkTo(parent.top, margin = 10.dp)
                end.linkTo(parent.end, margin = 20.dp)
            }) {
            }
        }*/

        FlowRow(
            horizontalArrangement = Arrangement.spacedBy(10.dp),
            verticalArrangement = Arrangement.spacedBy(10.dp)
        ) {
            list.forEach {
                Text(
                    text = it, modifier = Modifier
                        .background(Color.Yellow, RoundedCornerShape(15.dp))
                        .border(
                            1.dp,
                            Color.DarkGray, RoundedCornerShape(15.dp)
                        )
                        .padding(horizontal = 8.dp, vertical = 3.dp)
                )
            }
        }
        var time by remember {
            mutableStateOf(SimpleDateFormat("yyyy-MM-dd").format(System.currentTimeMillis()))
        }

        var isShow by remember {
            mutableStateOf(false)
        }

        val pickerState = rememberDatePickerState()
        OutlinedTextField(value = time, onValueChange = {}, readOnly = true,
            trailingIcon = {
                Icon(
                    imageVector = Icons.Default.DateRange,
                    contentDescription = "",
                    Modifier.clickable {
                        isShow=true
                    })
            })

        if(isShow){
            DatePickerDialog(onDismissRequest = { isShow=false }, confirmButton = {
                Text(text = "确定",Modifier.clickable {
                    time=SimpleDateFormat("yyyy-MM-dd").format(pickerState.selectedDateMillis)
                    isShow=false
                })
            }) {
                DatePicker(state = pickerState)
            }
        }
    }
}
